#include <iostream>
#include <ctime>
#include <unistd.h>
#include "ringQueue.hpp"

void* Consumer(void* args)
{
    RingQueue<int>* rq = (RingQueue<int>*)(args);

    while(true)
    {
        sleep(1);
        int data;
        rq->Pop(data);
        std::cout << "Comsumer: " << data << " [" << pthread_self() << "]" << std::endl;
    }

    return nullptr;
}

void* Productor(void* args)
{
    RingQueue<int>* rq = (RingQueue<int>*)(args);

    while(true)
    {
        int data = rand() % 100 + 1;
        rq->Push(data);
        std::cout << "Productor: " << data << " [" << pthread_self() << "]" << std::endl;
    }

    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));

    RingQueue<int>* rq = new RingQueue<int>();
    pthread_t c[3], p[2];
    pthread_create(c, nullptr, Consumer, (void*)rq);
    pthread_create(c+1, nullptr, Consumer, (void*)rq);
    pthread_create(c+2, nullptr, Consumer, (void*)rq);

    pthread_create(p, nullptr, Productor, (void*)rq);
    pthread_create(p+1, nullptr, Productor, (void*)rq);

    for(int i = 0; i < 3; i++) pthread_join(c[i], nullptr);
    for(int i = 0; i < 2; i++) pthread_join(p[i], nullptr);
    delete rq;

    return 0;
}